package com.myk.game.gobangchess.rooms;

import java.util.ArrayList;
import java.util.List;

import com.myk.game.gobangchess.CustomBottom;
import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.constants.BoardStyleData;
import com.myk.game.gobangchess.constants.WinMode;
import com.myk.game.gobangchess.signs.AroundSign;

/**
 * 合群之落
 */
public class HeQunZhiLuo extends Room {
    public HeQunZhiLuo(String roomName) {
        super(roomName, "\t\t合群之落", "合群之落", BoardStyleData.getGoBangStyle(12, 12), true,
                true);
    }

    int[][] color = new int[12][12];

    boolean passed=false;
    CustomBottom pass = new CustomBottom(this, "跳过走棋", (player) -> {
        if(passed){
            doEnd();
        }
        else{
            passed=true;
            noticeChangeMove();
        }
    });

    void noticeChangeMove(){
        if (cur == 1) {
            player_black.clearCustomBottom();
            player_black.dispatchCustomBottom();
            player_white.clearCustomBottom();
            player_white.registerCustomBottom(pass);
            player_white.dispatchCustomBottom();
            noticeWhiteMove();
        } else {
            player_white.clearCustomBottom();
            player_white.dispatchCustomBottom();
            player_black.clearCustomBottom();
            player_black.registerCustomBottom(pass);
            player_black.dispatchCustomBottom();
            noticeBlackMove();
        }
    }
    @Override
    public String getRuleText() {
        return "12路棋盘\n" + //
                "轮流落1子，形成三连时，将三连所处的九宫格染为自己的颜色（颜色可被覆盖）\n" +
                "终盘时，每格染色得1分，分高者胜\n" +
                "\n" +
                "【竞技版】（默认）\n" +
                "1.先手方需要倒贴0分（日后有可能会改这个参数）\n" +
                "2.增加「跳过走棋」功能按钮（即PASS）";
    }

    @Override
    public void resetGrids() {
        super.resetGrids();
        blackScore = 0;
        whiteScore = 0;
        passed=false;
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                color[i][j] = 0;
            }
        }
    }

    @Override
    public void doOver(int winMode, String winMsg) {
        if (player_black != null) {
            player_black.clearCustomBottom();
            player_black.dispatchCustomBottom();
        }
        if (player_white != null) {
            player_white.clearCustomBottom();
            player_white.dispatchCustomBottom();
        }
        super.doOver(winMode, winMsg);
    }

    final static int[][] dirs = {
            { 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }
    };

    void doEnd() {
        if (blackScore > whiteScore) {
            doOver(WinMode.BLACK_WIN,
                    player_black.getNickNameOrTemp() + "执黑棋战胜" + player_white.getNickNameOrTemp());
        } else if (blackScore < whiteScore) {
            doOver(WinMode.WHITE_WIN,
                    player_white.getNickNameOrTemp() + "执白棋战胜" + player_black.getNickNameOrTemp());
        } else {
            doOver(WinMode.BOTH_DRAW,
                    player_white.getNickNameOrTemp() + "与" + player_black.getNickNameOrTemp() + "平手");
        }
    }

    @Override
    public void doRule(int row, int col, int value) {
        if (curGrids[row][col] == 0) { // 只能落在空位置
            signList.clear();
            curGrids[row][col] = value;
            stepAdd();
            for (int[] dir : dirs) {
                int start = -1, end = 1;
                while (true) {
                    int r = row + dir[0] * start, c = col + dir[1] * start;
                    if (r < 0 || c < 0 || r > 11 || c > 11 || start<-2 || curGrids[r][c] != value) {
                        break;
                    }
                    start--;
                }
                while (true) {
                    int r = row + dir[0] * end, c = col + dir[1] * end;
                    if (r < 0 || c < 0 || r > 11 || c > 11 ||end>2|| curGrids[r][c] != value) {
                        break;
                    }
                    end++;
                }
                for (int i = start + 2; i < end-1; i++) {
                    for (int r = row + dir[0] * i - 1; r <= row + dir[0] * i + 1; r++) {
                        for (int c = col + dir[1] * i - 1; c <= col + dir[1] * i + 1; c++) {
                            if (r >= 0 && c >= 0 && r < 12 && c < 12) {
                                if (color[r][c] != value) {
                                    if (color[r][c] == 0) {
                                        if (value == 1) {
                                            blackScore++;
                                        } else {
                                            whiteScore++;
                                        }
                                    } else{
                                        if (value == 1) {
                                            blackScore++;
                                            whiteScore--;
                                        } else {
                                            whiteScore++;
                                            blackScore--;
                                        }
                                    }
                                    color[r][c] = value;
                                }
                            }
                        }
                    }
                }
            }
            ArrayList<Integer> b = new ArrayList<>(), w = new ArrayList<>();
            for (int i = 0; i < color.length; i++) {
                for (int j = 0; j < color[i].length; j++) {
                    if (color[i][j] == 1) {
                        b.add(getIndex(i, j));
                    } else if (color[i][j] == 2) {
                        w.add(getIndex(i, j));
                    }
                }
            }
            if (b.size() > 0) {
                signList.add(new AroundSign(b, "#99cfeaf1", "#ffcfeaf1", 0.5f));
            }
            if (w.size() > 0) {
                signList.add(new AroundSign(w, "#99f8cde0", "#fff8cde0", 0.5f));
            }
            if (step == 144) {
                doEnd();
            } else{
                passed=false;
                noticeChangeMove();
            }
        }
    }
}
